package com.example.springsecurity.service.impl;

import com.example.springsecurity.domain.Announcement;
import com.example.springsecurity.domain.News;
import com.example.springsecurity.domain.RoProject;
import com.example.springsecurity.domain.UploadCentre;
import com.example.springsecurity.mapper.ExperimentMapper;
import com.example.springsecurity.mapper.ExperimentPortalMapper;
import com.example.springsecurity.service.ExperimentPortalService;
import com.example.springsecurity.utils.FileUploadAndDownload;
import com.example.springsecurity.utils.ResultBean;
import com.example.springsecurity.utils.redis.RedisCache;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Service
public class ExperimentPortalServiceImpl implements ExperimentPortalService {


    @Autowired
    public ExperimentPortalMapper experimentPortalMapper;

    private Logger logger= LoggerFactory.getLogger(ExperimentPortalServiceImpl.class);

    @Autowired
    FileUploadAndDownload fileUploadAndDownload;

    @Autowired
    public RedisCache redisCache;

    @Override
    public ResultBean getPortalExperiment(Integer collegeId,Integer pageNum,Integer pageSize,String projectName) {
        try {
            PageHelper.startPage(pageNum,pageSize);
            List<RoProject> roProjectList=experimentPortalMapper.getPortalExperiment(collegeId,projectName);
            roProjectList.forEach(roProject -> {
                if(redisCache.getCacheObject("searchExperiment:"+roProject.getProjectId())==null){
                    roProject.setSearchExperimentNums(0);
                }else{
                    roProject.setSearchExperimentNums(redisCache.getCacheObject("searchExperiment:"+roProject.getProjectId()));
                }
            });
            PageInfo<RoProject> roProjectPageInfo=new PageInfo<>(roProjectList);
            return ResultBean.success(roProjectPageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultBean.error("获取实验项目失败");
        }
    }


    @Override
    public ResultBean getNews(Integer pageNum, Integer pageSize, Integer year, Integer month,String newsTitle) {
        try {
            PageHelper.startPage(pageNum,pageSize);
            List<News> newsList=experimentPortalMapper.getNews(year,month,newsTitle);
            PageInfo<News> newsPageInfo=new PageInfo<>(newsList);
            return ResultBean.success(newsPageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultBean.error("获取新闻失败");
        }
    }


    @Override
    public ResultBean addNews(News news) {
        try {
            Integer addNewsStatus=experimentPortalMapper.addNews(news);
            if(addNewsStatus!=0){
                return  ResultBean.success("成功添加新闻");
            }else{
                return ResultBean.error("添加新闻失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResultBean.error("添加新闻失败");
        }
    }


    @Override
    public ResultBean updateNews(News news) {
        try {
            Integer updateNewsStatus=experimentPortalMapper.updateNews(news);
            if(updateNewsStatus!=0){
                return ResultBean.success("成功修改新闻信息");
            }else{
                return ResultBean.error("修改新闻信息失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResultBean.error("修改新闻信息失败");
        }
    }


    @Override
    public ResultBean getAnnouncement(Integer pageNum, Integer pageSize, Integer year, Integer month,String announcementTitle) {
        try {
            PageHelper.startPage(pageNum,pageSize);
            List<Announcement> announcementList=experimentPortalMapper.getAnnouncement(year,month,announcementTitle);
            PageInfo<Announcement> announcementPageInfo=new PageInfo<>(announcementList);
            return ResultBean.success(announcementPageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultBean.error("获取公告有误");
        }
    }

    @Override
    public ResultBean addAnnouncement(Announcement announcement) {
        try {
            Integer addAnnouncementStatus=experimentPortalMapper.addAnnouncement(announcement);
            if(addAnnouncementStatus!=0){
                return ResultBean.success("成功添加公告!");
            }else{
                return ResultBean.error("添加公告失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResultBean.error("添加公告失败");
        }
    }


    @Override
    public ResultBean updateAnnouncement(Announcement announcement) {
        try {
            Integer updateAnnouncementStatus=experimentPortalMapper.updateAnnouncement(announcement);
            if(updateAnnouncementStatus!=0){
                return ResultBean.success("成功修改公告");
            }else{
                return ResultBean.error("修改公告失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResultBean.error("修改公告失败");
        }
    }


    @Override
    public ResultBean getUploadInfo(Integer pageNum, Integer pageSize, Integer uploadCategory,String uploadIntroduce) {
        try {
            PageHelper.startPage(pageNum,pageSize);
            List<UploadCentre> uploadCentreList=experimentPortalMapper.getUploadInfo(uploadCategory,uploadIntroduce);
            PageInfo<UploadCentre> uploadCentrePageInfo=new PageInfo<>(uploadCentreList);
            return ResultBean.success(uploadCentrePageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            logger.info("获取下载信息失败{}",e.getStackTrace());
            return ResultBean.error("获取下载信息失败");
        }
    }


    @Override
    public ResultBean updateUploadInfo(UploadCentre uploadCentre) {
        try {
            Integer updateUploadInfoStatus=experimentPortalMapper.updateUploadInfo(uploadCentre);
            if(updateUploadInfoStatus!=0){
                return ResultBean.success("修改成功");
            }else{
                return ResultBean.error("修改失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.info("修改信息失败",e.getStackTrace());
            return ResultBean.error("修改信息失败");
        }
    }


    @Override
    public ResultBean addUploadInfo(MultipartFile file, UploadCentre uploadCentre) {
        try {
            if(null!=file){
                String uploadPath=fileUploadAndDownload.upload(file);
                uploadCentre.setUploadPath(uploadPath);
            }
            Integer addUploadInfoStatus=experimentPortalMapper.addUploadInfo(uploadCentre);
            if(addUploadInfoStatus!=0){
                return ResultBean.success("上传成功！");
            }else{
                return ResultBean.error("上传失败！");
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.info("上传失败",e.getStackTrace());
            return ResultBean.error("上传失败");
        }
    }


    @Override
    public void downloadInfo(Integer uploadCentreId, HttpServletRequest request, HttpServletResponse response) {
        String uploadPath=experimentPortalMapper.downloadInfo(uploadCentreId);
        try {
            if(uploadPath!=null){
                fileUploadAndDownload.download(request,response,uploadPath);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    @Override
    public ResultBean getNewsDetail(Integer newsId) {
        News news=experimentPortalMapper.getNewsDetail(newsId);
        return ResultBean.success(news);
    }

    @Override
    public ResultBean deleteNews(String newsId) {
        String[] newsIds=newsId.split(",");
        int[] newsIdArray=new int[newsIds.length];
        for (int i = 0; i <newsIds.length; i++) {
            newsIdArray[i]= Integer.parseInt(newsIds[i]);
        }
        Integer deleteNewsStatus=experimentPortalMapper.deleteNews(newsIdArray);
        if(deleteNewsStatus!=0){
            return ResultBean.success("成功删除该新闻信息!");
        }else{
            return ResultBean.error("删除该新闻信息失败！");
        }
    }


    @Override
    public ResultBean getAnnouncementId(Integer announcementId) {
        Announcement announcement=experimentPortalMapper.getAnnouncementId(announcementId);
        return ResultBean.success(announcement);
    }


    @Override
    public ResultBean deleteAnnouncement(String announcementId) {
        String[] announcementIds=announcementId.split(",");
        int[] announcementIdsArray=new int[announcementIds.length];
        for (int i = 0; i <announcementIds.length; i++) {
            announcementIdsArray[i]= Integer.parseInt(announcementIds[i]);
        }
        Integer deleteAnnouncement=experimentPortalMapper.deleteAnnouncement(announcementIdsArray);
        if(deleteAnnouncement!=0){
            return ResultBean.success("成功删除该公告信息");
        }else{
            return ResultBean.success("删除该公告信息失败");
        }
    }

    @Override
    public ResultBean deleteDownTool(String uploadCentreId) {
        String[] uploadCentreIds=uploadCentreId.split(",");
        int[] uploadCentreIdsArray=new int[uploadCentreIds.length];
        for (int i = 0; i <uploadCentreIds.length; i++) {
            uploadCentreIdsArray[i]= Integer.parseInt(uploadCentreIds[i]);
        }
        Integer deleteDownToolStatus=experimentPortalMapper.deleteDownTool(uploadCentreIdsArray);
        if(deleteDownToolStatus!=0){
            return ResultBean.success("成功删除该下载工具");
        }else{
            return ResultBean.error("删除该下载工具失败");
        }
    }
}
